<template>
  <card>
    <!--编辑页面-->
      <Form ref="formItem" :model="formItem" :label-width="80">        
          <input type="hidden" v-model="formItem.attrgroupid"></input>
          <input type="hidden" v-model="formItem.cateid"></input>
          <input type="hidden" v-model="formItem.createTime"></input>
          <input type="hidden" v-model="formItem.updateTime"></input>
          <FormItem label="名称">
            <Input v-model="formItem.attrgroupname"></Input>
          </FormItem>
          <FormItem label="排序" >
            <Input type="text" v-model="formItem.displayorder" number></Input>
          </FormItem>
          <FormItem label="显示">            
            <i-Switch v-model="formItem.display" :true-value=0 :false-value=1>
              <span slot="open">开</span>
              <span slot="close">关</span>
            </i-Switch>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleSubmit('formItem')">提交</Button>
            <Button style="margin-left: 8px" @click="handleCloseTag">取消</Button>
          </FormItem>
      </Form>  
  </card>
</template>

<script>
import { mapMutations } from 'vuex'
import Bus from '@/libs/bus.js'
import { getAttributeGroupsModel } from "@/api/data";
import { addAttributeGroups} from "@/api/data";
import { editAttributeGroups } from "@/api/data";
import photoSingle from './photoSingle.vue'
export default {
  name:'attribute_groups_edit',
  components: {
    photoSingle
  },
  data() {
    return {      
      show:true,
      formItem: {
        "attrgroupid": 1,
        "cateid": 1,
        "attrgroupname": "twest",
        "displayorder": 0,
        "display": 0,
        "createTime": 1544519720000,
        "updateTime": 1544519720000
      }
    };
  },
  methods: {
     ...mapMutations([
      'closeTag'
    ]),
    getData() {
      getAttributeGroupsModel({Id:this.formItem.attrgroupid}).then(res => {
        this.formItem = res.data.data;
      });
    },
    
    handleCloseTag () {
      debugger;
      this.closeTag({
        name: 'attribute_groups_edit',
        query:{id:this.$route.query.id,cateid:this.formItem.cateid}
      })
    },
    handleSubmit(name) {
      debugger;
      if(this.formItem.attrgroupid!=0){
        //编辑
        editAttributeGroups(this.formItem).then(res => {
          this.$Message.success(res.data.data);
          this.bus();
        });
      } else {
        //添加
        addAttributeGroups(this.formItem).then(res => {
          this.$Message.success(res.data.data);
          this.bus();
        });
      }      
      this.handleCloseTag();
    },
    bus () {
      Bus.$emit('attribute_groups_edit', 'true')
    }
  },
  mounted() {
    var id=this.$route.query.id;
    var cateid=this.$route.query.cateid;
    this.formItem.cateid=cateid;
    this.formItem.attrgroupid=id;
    if(id>0){      
      this.getData();
    }
  }
};
</script>



